package com.demo.controller;

import com.demo.pojo.MyConfig;
import com.demo.pojo.Stu;
import com.demo.pojo.Student;
import com.demo.utils.JSONResult;
import com.demo.utils.MyAsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/*
 * @RestController 注解等价于 @ResponseBody ＋ @Controller
 * @RestController 和 @Controller 的共同点是都用来表示 Spring 某个类是否可以接收HTTP请求,
 * @RestController 无法返回指定页面, 而@Controller可以, 前者可以直接返回数据, 后者需要@ResponseBody辅助
 * */
//@Controller
@RestController
@Slf4j
public class HelloController {
    @Autowired // 将 Stu 注入到字段中
    private Stu stu;

    @Autowired
    private MyConfig myConfig;

    @Autowired
    private Student student;

//    @Autowired
//    private MyAsyncTask myAsyncTask;

    @Value("${self.custom.config.sdkSecrect}")
    private String sdkSecrect;
    @Value("${self.custom.config.host}")
    private String host;

    // 作用就是映射URL路径, 将 http 的请求地址映射到控制器(controller)类的处理方法上(hello上)
    @RequestMapping("hello")
//    @ResponseBody
    public String hello() {
        return "Hello World...";
    }

    @GetMapping("getStu")
    public JSONResult getStu() {
        return JSONResult.ok(stu);
    }

    @GetMapping("getConfig")
    public Object getConfig() {
        return myConfig;
    }

    @GetMapping("getYmlConfig")
    public Object getYmlConfig() {
        return sdkSecrect + host;
    }

    @GetMapping("getStudent")
    public Object getStudent() {
        student.setName("test lombok");
        log.info(student.toString());
        return student.name;
    }

    @PostMapping("upload")
    public String upload(MultipartFile file) throws IOException {
        // 接收到文件, 并转移到具体的路径目录下
        file.transferTo(new File("/Users/zichong/Desktop/ssm/" + file.getOriginalFilename()));
        return "success";
    }

//    @GetMapping("runAsyncTask")
//    public String testAsyncTask() {
//        myAsyncTask.publishMsg();
//        log.info("跳过异步任务的执行.");
//        return "myAsyncTask";
//    }
}